// components/kno-item/index.js
import {
  VIDEO_COVER,
} from "../../service/base_service"
import {
  articleDetail
} from "../../service/jb_article_service"
import {
  addCollect,
  cancelCollect,
} from "../../service/jb_common_service"
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    //video:视频，article:文章
    knoType: {
      type: String,
      value: "video",
    },
    knoData: {
      type: Object,
      value: {},
    },
    index: {
      type: Number,
      value: 0
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    videoCover: VIDEO_COVER,
    isHandleRequest: false,//是否已经调用过了,调用一次详情，播放一次调用一次就好，防止多次调用
  },

  /**
   * 组件的方法列表
   */
  methods: {

    pushToArticleDetail() {
      // app.globalData.knoData = this.properties.knoData;
      wx.navigateTo({
        url: '/pages/richPage/richPage?id=' + this.properties.knoData.articleId,
      })
    },
    pushToVideoDetail() {
      wx.navigateTo({
        url: '/pages/videoPlay/videoPlay?id=' + this.properties.knoData.articleId,
      })
    },
    //视频开始播放
    videoPlayAction() {
      // if (!this.data.isHandleRequest) {
      //   articleDetail(this.data.properties.id).then(res => {
      //     if (res.code == 200) {
      //       this.setData({
      //         isHandleRequest: true
      //       })
      //     }
      //   }).catch(err => {

      //   })
      // }

    },
    //收藏操作
    collectAction() {
      this.triggerEvent('collectAction', { data: this.properties.knoData, index: this.properties.index })
    },

    //加入收藏
    handlerAddCollect() {
      wx.showLoading();
      addCollect({
        category: "article",
        id: this.properties.knoData.id
      }).then(res => {
        wx.hideLoading()
        if (res.code == 200) {
          this.setData({
            'bidProductData.userCollect': true,
          })
        }
      }).catch(err => {
        wx.hideLoading()
        wx.showToast({
          title: err || '收藏失败',
          icon: "none"
        })
      })
    },
    //取消收藏
    handlerCancelCollect() {
      wx.showLoading();
      cancelCollect(this.data.productDetail.id).then(res => {
        wx.hideLoading()
        if (res.code == 200) {
          this.setData({
            'bidProductData.userCollect': false,
          })
        }
      }).catch(err => {
        wx.hideLoading()
        wx.showToast({
          title: err || '操作失败',
          icon: "none"
        })
      })
    },
  }
})
